package chamber.coffeeshop;

/**
 * Created by Gerardo on 1/15/14.
 */
public class CoffeeMachine extends Thread {
    static String coffeeMade = null;
    static final Object lock = new Object();
    private static int coffeeNumber = 1;

    void makeCoffee() {
        synchronized (CoffeeMachine.lock) {
            if (coffeeMade != null) {
                try {
                    System.out.println("coffee machine: waiting for waiter notification to deliver to coffee");
                    CoffeeMachine.lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            coffeeMade = "Coffee No. " + coffeeNumber++;
            System.out.println("Coffee Machine says: made " + coffeeMade);

            CoffeeMachine.lock.notifyAll();
            System.out.println("coffee machine: notifying waiter to pick up coffee");
        }
    }

    @Override
    public void run() {
        while (true) {
            makeCoffee();
            try {
                System.out.println("coffee machine: making another coffee");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
